<template>
	<uv-checkbox-group v-model="localCheckbox" shape="circle" activeColor="#AD362B" style="width: 100%;" v-if="list.length">
		<view class="item" v-for="(e,i) in list" :key="i">
			<view class="top">
				<view class="flex align-center">
					<image :src="e.brand_logo" class="logo"/>
					<text class="shopname">{{e.merchant_name||'暂无名称'}}</text>
				</view>
				<uv-icon name="arrow-right" color="#DBDBDB" size="14" />
			</view>
			
			<uv-line color="#EEEEEE" />
			
			<view class="middle" v-for="(a, i) in e.cart_product" :key="a.id">
				<uv-checkbox :name="a.id" />
				<view class="info">
					<image :src="a.product_image" class="product_pic"/>
					<view class="right">
						<view>
							<text class="pname">{{a.product_name}}</text>
							<uv-gap height="5" bgColor="transparent" v-if="a.spec_values"/>
							<view class="tags" v-if="a.spec_values">{{a.spec_values}}</view>
						</view>
						<view class="jiage">
							<view class="flex" style="align-items: flex-end;">
								<text>¥</text>
								<text>{{a.price}}</text>
								<text>{{a.market_price}}</text>
							</view>
							<uv-number-box v-model="a.cart_num" button-size="14" @change="setsNum(a, e.value)">
							</uv-number-box>
						</view>
					</view>
				</view>
			</view>
		</view>
	</uv-checkbox-group>
</template>

<script setup>
	import { ref, computed } from 'vue';
	import { setGoodsNum } from '@/api'
	
	const emit = defineEmits(['update:checkbox'])
	
	const props = defineProps({
		list: {
			type: Array,
			default: () => []
		},
		checkbox: {
		    type: Array,
		    default: () => []
		}
	})
	
	const localCheckbox = computed({
	  get: () => props.checkbox,
	  set: (value) => emit('update:checkbox', value)
	})

	const checkboxValue = ref([])
	
	
	const setsNum = async (item, value) => {
		let type = 0
		if (item.cart_num > value){
			type = 2
		} else {
			type = 1
		}
		item.cart_num = value
		uni.$uv.debounce(async () => {
			const { id, cart_num } = item
			await setGoodsNum({id, type, cart_num})
		})
	}
</script>

<style lang="scss" scoped>
	.item {
		width: 100%;
		margin-top: 30upx;
		background-color: #fff;
		.top {
			display: flex;
			padding: 16upx 30upx;
			align-items: center;
			justify-content: space-between;
		}
		.logo {
			width: 40upx;
			height: 40upx;
			border-radius: 6upx;
			margin-right: 10upx;
		}
		.shopname {
			font-size: 28upx;
			color: #333;
		}
		.middle {
			display: flex;
			padding: 30upx;
			align-items: center;
			.info {
				flex: 1;
				display: flex;
				margin-left: 10upx;
				.product_pic {
					width: 120upx;
					height: 120upx;
					border-radius: 6upx;
					margin-right: 20upx;
					flex-shrink: 0;
				}
				.right {
					width: 100%;
					display: flex;
					margin-left: 20upx;
					justify-content: space-between;
					flex-direction: column;
					.pname {
						font-size: 28upx;
						color: #333;
					}
					.tags {
						background:#F2F2F2;
						color: #333;
						display: inline-flex;
						border-radius: 6upx;
						font-size: 24upx;
						padding: 4upx 15upx;
					}
				}
			}
			.xuanze1 {
				width: 70%;
				display: flex;
				color: #444446;
				margin-top: 10upx;
				padding: 2upx 6upx;
				font-size: 24upx;
				border-radius: 4upx;
				align-items: center;
				justify-content: space-between;
				border: 1px solid #D2D2D2;
			}
			.xuanze2 {
				color: #5B595C;
				font-size: 24upx;
				padding: 4upx 10upx;
				display: inline-block;
				margin-top: 10upx;
				background-color: #F5F3F6;
			}
			.xuanze3 {
				width: 90%;
				display: flex;
				color: #444446;
				padding: 2upx 6upx;
				font-size: 24upx;
				border-radius: 4upx;
				align-items: center;
				justify-content: space-between;
			}
			.jiage {
				display: flex;
				align-items: end;
				color: #CC2B24;
				font-size: 24upx;
				justify-content: space-between;
				text {
					&:nth-child(1) {
						font-size: 22upx;
						margin-bottom: 3upx;
						margin-right: 5upx;
						align-items: flex-end;
					}
					&:nth-child(2) {
						font-size: 30upx;
					}
					&:nth-child(3) {
						font-size: 24upx;
						color: #999999;
						margin-left: 10upx;
						text-decoration: line-through;
						align-items: flex-end;
					}
				}
				.num {
					width: 60upx;
					margin: 0 10upx;
					padding: 6upx 0;
					background-color: #EBECEE;
				}
			}
		}
	}
	
</style>